package top.hzy520.admin.carousel.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import top.hzy520.admin.carousel.entity.ConstsSiteCarousel;
import top.hzy520.admin.carousel.service.CarouselService;
import top.hzy520.admin.common.constants.Constants;
import top.hzy520.admin.common.page.Pagination;
import top.hzy520.admin.common.resp.ApiResult;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 9:26 2019/6/5
 */
@Controller
public class CarouselController {
    @Autowired
    @Qualifier("carouselServiceImpl")
    private CarouselService carouselService;

    @GetMapping("/index/carousel/pc")
     public String carousel(){
        return "carousel/carousel";
    }

    @RequestMapping("/carousel/list")
    @ResponseBody
    public Pagination<ConstsSiteCarousel> constsSiteCarouselPagination(int offset,int limit){
     Pagination<ConstsSiteCarousel> pagination=new Pagination<>();
      List<ConstsSiteCarousel> constsSiteCarouselList=carouselService.constsSiteCarouselPagination(offset,limit);
      int count=carouselService.constsSiteCarouselPaginationCount();
      pagination.setRows(constsSiteCarouselList);
      pagination.setTotal(count);
     return pagination;
    }

    @GetMapping("/carousel/add")
    public String carouselAdd(){
        return "carousel/add";
    }
    @GetMapping("/carousel/edit/{id}")
    public String carouselEdit(@PathVariable("id") Integer id, Model model){
        ConstsSiteCarousel constsSiteCarousel=carouselService.findCarouselById(id);
        model.addAttribute("carousel",constsSiteCarousel);
        return "carousel/edit";
    }
    @PostMapping("/carousel/add")
    @ResponseBody
    public ApiResult carouselAddPost(ConstsSiteCarousel carousel){
        ApiResult apiResult=new ApiResult(Constants.RESP_STATUS_OK,"添加成功");
       int result=carouselService.carouselAddPost(carousel);
       if (result!=1){
           apiResult.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
           apiResult.setMessage("添加失败");
       }
        return apiResult;
    }
    @PostMapping("/carousel/edit")
    @ResponseBody
    public ApiResult carouselEditPost(ConstsSiteCarousel carousel){
        ApiResult apiResult=new ApiResult(Constants.RESP_STATUS_OK,"修改成功");
        int result=carouselService.carouselEditPost(carousel);
        if (result!=1){
            apiResult.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            apiResult.setMessage("修改失败");
        }
        return apiResult;
    }
    @PostMapping("/carousel/changeStatus")
    @ResponseBody
    public ApiResult carouselChangeStatus(ConstsSiteCarousel carousel){
        ApiResult apiResult=new ApiResult(Constants.RESP_STATUS_OK,"修改状态成功");
      int result=carouselService.carouselChangeStatus(carousel);
      if (result!=1){
          apiResult.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
          apiResult.setMessage("修改状态失败");
      }
        return apiResult;
    }
    @PostMapping("/carousel/remove")
    @ResponseBody
    public ApiResult carouselRemove(String ids){
        ApiResult apiResult=new ApiResult(Constants.RESP_STATUS_OK,"删除成功");
        if (ids!=null){
            String[] a=ids.split(",");
            List<Integer> idList=new ArrayList<>();
            for (String id:a){
                idList.add(Integer.valueOf(id));
            }
          carouselService.carouselRemove(idList);
        }
        return apiResult;
    }
}
